<template>
  <div id="myChart" :style="{width: '100%', height: '500px'}"></div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
var { mapState, mapActions } = createNamespacedHelpers("merchant");
export default {
  name: "inventory",
  data() {
    return {
      msg: "My Inventory"
    };
  },
  created() {
    this.getAllInventoryAsync(this.merchantId);
  },
  watch: {
    "inventory.allInventory"() {
      this.drawLine();
    }
  },
  computed: {
    ...mapState(["inventory"]),
    echartsXData() {
      var arr;
      arr = this.inventory.allInventory.map(item => {
        return item.commodityName;
      });
      return arr;
    },
    echartsYData() {
      var arr = [];
      arr = this.inventory.allInventory.map(item => {
        return Number(item.inventoryNumbers);
      });
      return arr;
    }
  },
  methods: {
    ...mapActions(["getAllInventoryAsync"]),
    drawLine() {
      // 基于准备好的dom，初始化echarts实例
      //   myChart.clear();
      let myChart = this.$echarts.init(document.getElementById("myChart"));
      //    myChart.clear();

      // 绘制图表
      myChart.setOption(
        {
          title: { text: "库存统计" },
          tooltip: {},
          // x轴
          xAxis: {
            //   data: ["西瓜", "苹果", "梨子", "火龙果", "香蕉", "榴莲"],
            //   data: this.inventory.inventoryList.map(item => {
            //     return item.commodityName;
            //   }),
            data: this.echartsXData,
            axisLabel: {
              interval: 0,
              rotate: 15, //倾斜度 -90 至 90 默认为0
              margin: 2,
              textStyle: {
                fontWeight: "bolder",
                color: "#000000"
              }
            },
            type: "category"
          },
          // y轴
          yAxis: {
            type: "value"
          },
          // 鼠标放在柱状图上的效果
          series: [
            {
              name: "库存量",
              type: "bar",
              data: this.echartsYData
            }
          ]
        },
        true
      );
    }
  }
};
</script>

<style>
</style>